package com.ledim.activity.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.MainActivity;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LedimSpUtils;
import com.letv.android.client.webviewsdklib.AbsWebViewActivity;
import com.letv.android.young.client.R;

public class LedimWebViewActivity extends AbsWebViewActivity {

    /**
     * 传递参数
     */
    public Bundle intentData;
    /**
     * 参数传递标示
     */
    public static final String PARAM_INTENT = "intentData";
    private boolean isFromPushMsg;
    private ImageView mBack;
    private TextView mTitle;
    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        mBack = (ImageView) findViewById(R.id.iv_back);
        mBack.setVisibility(View.VISIBLE);
        mTitle = (TextView) findViewById(R.id.tv_title);
        webView = (WebView) findViewById(R.id.webview_webView);
        webView.setWebViewClient(new WebViewClient() { //通过webView打开链接，不调用系统浏览器

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }

        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mBack.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        
        if (savedInstanceState == null) {
            intentData = getIntent().getExtras();
        } else {
            intentData = savedInstanceState.getBundle(PARAM_INTENT);
        }
        isFromPushMsg = getIntent().getBooleanExtra(ConstantUtil.FROM_PUSH_MSG, false);
        if (isFromPushMsg) {
            int pushMsgCount = LedimSpUtils.getPushMgsCount();
            if (pushMsgCount > 0) {
                pushMsgCount--;
                LedimSpUtils.setPushMgsCount(pushMsgCount);
            }
        }
        Bundle bundle = intentData != null && intentData.getBundle(PARAM_INTENT) != null
                        ? intentData.getBundle(PARAM_INTENT) : intentData;
        String url = bundle.getString(ConstantUtil.URL);

        String titleStr = bundle.getString(ConstantUtil.TITLE);
        if (!TextUtils.isEmpty(titleStr)) {
            mTitle.setText(titleStr);
        }
        
        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }
    }

    @Override
    protected WebView inflateCustomWebView() {
        return webView;
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isFromPushMsg && MainActivity.getInstance() == null) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
            finish();
        }
        return true;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onResume() {

        super.onResume();
        webView.onResume();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onPause() {

        super.onPause();
        webView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.stopLoading();
        webView.loadData("", "text/html", "utf-8");
    }
}
